<?php
class ItemsController extends AppController {

	var $name = 'Items';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Item->recursive = 0;
		$this->set('items', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Item.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('item', $this->Item->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$user = $this->Auth->user();
			$this->data['Item']['user_id'] = $user['User']['id'];

			App::import('Model', 'Image');
			if (is_uploaded_file($this->data['Image']['0']['upload_image']['tmp_name'])) {
				$this->data['Image']['0'] = $this->data['Image']['0']['upload_image'];
				$this->data['Image']['0']['upload_image'] = false;
				$this->data['Image']['0']['data'] = fread(fopen($this->data['Image']['0']['tmp_name'], 'r'), $this->data['Image']['0']['size']);
				$this->data['Image']['0']['thumbnail'] = fread(fopen($this->data['Image']['0']['tmp_name'], 'r'), $this->data['Image']['0']['size']);
			}

			$this->Item->create();
			if ($this->Item->saveAll($this->data, array('validate'=>'first'))) {
				$this->Session->setFlash(__('The Item has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Item could not be saved. Please, try again.', true));
			}
		}

		$images = $this->Item->Image->find('list');
		$conditions = $this->Item->Condition->find('list');
		$categories = $this->Item->Category->find('list');
		$users = $this->Item->User->find('list');
		$this->set(compact('images', 'conditions', 'categories', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Item', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Item->save($this->data)) {
				$this->Session->setFlash(__('The Item has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Item could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Item->read(null, $id);
		}
		$images = $this->Item->Image->find('list');
		$conditions = $this->Item->Condition->find('list');
		$categories = $this->Item->Category->find('list');
		$users = $this->Item->User->find('list');
		$this->set(compact('images','conditions','categories','users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Item', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Item->del($id)) {
			$this->Session->setFlash(__('Item deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function admin_index() {
		$this->Item->recursive = 0;
		$this->set('items', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Item.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('item', $this->Item->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Item->create();
			if ($this->Item->save($this->data)) {
				$this->Session->setFlash(__('The Item has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Item could not be saved. Please, try again.', true));
			}
		}
		$images = $this->Item->Image->find('list');
		$conditions = $this->Item->Condition->find('list');
		$categories = $this->Item->Category->find('list');
		$users = $this->Item->User->find('list');
		$this->set(compact('images', 'conditions', 'categories', 'users'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Item', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Item->save($this->data)) {
				$this->Session->setFlash(__('The Item has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Item could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Item->read(null, $id);
		}
		$images = $this->Item->Image->find('list');
		$conditions = $this->Item->Condition->find('list');
		$categories = $this->Item->Category->find('list');
		$users = $this->Item->User->find('list');
		$this->set(compact('images','conditions','categories','users'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Item', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Item->del($id)) {
			$this->Session->setFlash(__('Item deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>